import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.PriorityQueue;

//@author A0099409N
public class Storage {

	private static final String MSG_GENERIC_ERROR = "Error: %s. Please try again";
	private static PriorityQueue<Task> _event = new PriorityQueue<Task>();
	private static PriorityQueue<Task> _deadline = new PriorityQueue<Task>();
	private static PriorityQueue<Task> _misc = new PriorityQueue<Task>();
	private static PriorityQueue<Task> _eventDone = new PriorityQueue<Task>();
	private static PriorityQueue<Task> _deadlineDone = new PriorityQueue<Task>();
	private static PriorityQueue<Task> _miscDone = new PriorityQueue<Task>();
	private static PriorityQueue<Task> _reminder = new PriorityQueue<Task>();
	private static PriorityQueue<Task>[] _listOfQueues = new PriorityQueue[7];
	private static String _fileName = "snooze.ser";
	private static File _file;

	public Storage (){
		initialize();
	}

	public static void initialize(){
		_file = new File(_fileName);
		if (_file.exists()) {
			loadFromFile(_file);
		}
	}

	private static void loadFromFile(File file){
		try {
			FileInputStream fin = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fin);
			_listOfQueues =  (PriorityQueue<Task>[]) ois.readObject();
			updateQueue();
			ois.close();
		} catch (FileNotFoundException e) {
			showToUser(MSG_GENERIC_ERROR, e.getMessage());
		} catch (ClassNotFoundException e) {
			showToUser(MSG_GENERIC_ERROR, e.getMessage());
		} catch (IOException e) {
			showToUser(MSG_GENERIC_ERROR, e.getMessage());
		} 
	}

	private static void updateQueue(){
		_event = _listOfQueues[0];
		_deadline = _listOfQueues[1];
		_misc = _listOfQueues[2];
		_eventDone = _listOfQueues[3];
		_deadlineDone = _listOfQueues[4];
		_miscDone = _listOfQueues[5];
		_reminder = _listOfQueues[6];
	}

	private static void showToUser(String message, Object... args) {
		System.out.println(String.format(message, args));
	}

	public static void updateFile(PriorityQueue<Task> eList, PriorityQueue<Task> dList, PriorityQueue<Task> mList, 
			PriorityQueue<Task> doneEvent, PriorityQueue<Task> doneDeadline, 
			PriorityQueue<Task> doneMisc, PriorityQueue<Task> remindList) {
		_listOfQueues[0] = eList;
		_listOfQueues[1] = dList;
		_listOfQueues[2] = mList;
		_listOfQueues[3] = doneEvent;
		_listOfQueues[4] = doneDeadline;
		_listOfQueues[5] = doneMisc;
		_listOfQueues[6] = remindList;

		try {
			FileOutputStream fout = new FileOutputStream(_file);
			ObjectOutputStream oos = new ObjectOutputStream(fout);
			oos.writeObject(_listOfQueues);
			oos.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static PriorityQueue<Task> getEvent(){
		return _event;
	}

	public static PriorityQueue<Task> getDeadline(){
		return _deadline;
	}

	public static PriorityQueue<Task> getMisc(){
		return _misc;
	}

	public static PriorityQueue<Task> getDoneEvent(){
		return _eventDone;
	}

	public static PriorityQueue<Task> getDoneDeadline(){
		return _deadlineDone;
	}

	public static PriorityQueue<Task> getDoneMisc(){
		return _miscDone;
	}

	public static PriorityQueue<Task> getReminder(){
		return _reminder;
	}
}
